#pragma once
#include <fstream>

#include <unordered_map>
#include <string>

#include "Log.hpp"
#include "Common.hpp"
using namespace MyLogModule;

const std::string gName = "dict.txt";    //文件名
const std::string gPath = "./";

class Dict
{
private:
    bool LoadDictionary()
    {
        std::string file = _filePath + _fileName;
        std::ifstream ifs(file.c_str());
        if(!ifs.is_open())
        {
            LOG(ERROR) << "open file " << file << " error";
            return false;
        }

        //从文件中读取到内存中
        std::string line;
        while(std::getline(ifs,line))
        {
            std::string key;
            std::string value;
            if(SplitString(line,key,value))
            {
                _dictionary[key] = value;
            }
        }
        return true;
    }

public:
    Dict(std::string fileName = gName,std::string filePath = gPath)
        :_fileName(fileName)
        ,_filePath(filePath)
    {
        LoadDictionary();
    }

    std::string Translate(const std::string& word)
    {
        std::unordered_map<std::string,std::string>::iterator ret = _dictionary.find(word);
        if(ret == _dictionary.end())
            return "None";
        else
            return ret->second;
    }

    ~Dict()
    {}
private:
    std::unordered_map<std::string,std::string> _dictionary;
    std::string _fileName;
    std::string _filePath;
};  